package ciyuan.Item.SkillBook;

import ciyuan.Effect.Effects;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.ArrowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.TippedArrowItem;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

public class LightMarker extends SkillBookType {

    public LightMarker() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 45 * 1000);

        if (!playerEntity.level.isClientSide) {
            ItemStack itemstack = new ItemStack(ItemRegisterManager.jian2.get());
            ArrowItem arrowitem = (ArrowItem) itemstack.getItem();
            AbstractArrowEntity abstractarrowentity = arrowitem.createArrow(playerEntity.level, itemstack, playerEntity);
            abstractarrowentity.pickup = AbstractArrowEntity.PickupStatus.DISALLOWED;
            SkillUtils.addEntity(abstractarrowentity,playerEntity.level);
            abstractarrowentity.shootFromRotation(playerEntity, playerEntity.xRot, playerEntity.yRot, 0.0F, 1 * 3.0F, 1.0F);
        }

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "LightMarker";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.23");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.23");
    }

    public static class MarkArrow extends TippedArrowItem {
        public MarkArrow() {
            super(new Properties());
        }

        @Override
        public AbstractArrowEntity createArrow(World p_200887_1_, ItemStack p_200887_2_, LivingEntity p_200887_3_) {
            ArrowEntity arrowentity = new MarkArrowentity(p_200887_1_, p_200887_3_);
            arrowentity.setGlowing(true);
            return arrowentity;
        }

        public static class MarkArrowentity extends ArrowEntity {

            public MarkArrowentity(World p_i46758_1_, LivingEntity p_i46758_2_) {
                super(p_i46758_1_, p_i46758_2_);
            }

            private final long time = System.currentTimeMillis();

            @Override
            public void tick() {
                super.tick();
                if (System.currentTimeMillis()-time>20*1000) {
                    this.remove();
                }
            }

            @Override
            protected ItemStack getPickupItem() {
                return ItemStack.EMPTY;
            }

            @Override
            public int getColor() {
                return 16284963;
            }

            @Override
            protected void doPostHurtEffects(LivingEntity p_184548_1_) {
                super.doPostHurtEffects(p_184548_1_);
                p_184548_1_.addEffect(new EffectInstance(Effects.marked,300,0));
                p_184548_1_.addEffect(new EffectInstance(net.minecraft.potion.Effects.GLOWING,300,0));
                p_184548_1_.addEffect(new EffectInstance(net.minecraft.potion.Effects.MOVEMENT_SLOWDOWN,300,2));
            }
        }

    }
}
